{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "78148d30-ea43-4331-8ec6-08b63a59a2af",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 2 × 2矩阵的行列式值\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "413206d9-0cdc-42e7-ba48-d0946f7bf5ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "def determinant_2x2(matrix):\n",
    "    if len(matrix) != 2 or len(matrix[0]) != 2:\n",
    "        raise ValueError(\"Matrix must be 2x2\")\n",
    "    a = matrix[0][0]\n",
    "    b = matrix[0][1]\n",
    "    c = matrix[1][0]\n",
    "    d = matrix[1][1]\n",
    "    det = a*d - b*c\n",
    "    return det"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6ece61f-986c-450a-ac2b-033d1c6e26f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 示例用法\n",
    "A = [[3, 2],\n",
    "     [1, 4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "011c4329-bd93-4f46-9c29-e8d5e5885f1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵行列式: 10\n"
     ]
    }
   ],
   "source": [
    "det = determinant_2x2(A)\n",
    "print(\"矩阵行列式:\", det)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
